package com.api.core.service.impl;

import com.api.common.config.ApiConfig;
import com.api.common.enums.DataSourceType;
import com.api.core.domain.query.MonitoredLatestQuery;
import com.api.core.domain.query.intergration.ElementLatestScoreQuery;
import com.api.core.domain.query.intergration.RobotLatestScoreQuery;
import com.api.core.domain.query.tracing.intergration.IntergrationQuery;
import com.api.core.domain.vo.tracing.intergration.IntergrationVo;
import com.api.core.domain.vo.tree.TreeVo;
import com.api.core.mapper.IntergrationMapper;
import com.api.core.mapper.MonitoredMapper;
import com.api.core.service.IIntergrationService;
import com.api.framework.datasource.DynamicDataSourceContextHolder;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class IntergrationServiceImpl implements IIntergrationService {
    @Autowired
    IntergrationMapper intergrationMapper;
    @Autowired
    MonitoredMapper monitoredMapper;

    @Override
    public List<Double> generates(TreeVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<Double> dataList = intergrationMapper.generates(vo.getIntergrationTabName(), vo);

        DynamicDataSourceContextHolder.clearDataSourceType();
        return dataList;
    }

    @Override
    public List<IntergrationQuery> scroe(IntergrationVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        vo.initTableName();
        List<IntergrationQuery> dataList = intergrationMapper.scroe(vo);
        DynamicDataSourceContextHolder.clearDataSourceType();
        return dataList;
    }

    @Override
    public IntergrationQuery series(IntergrationVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        vo.initTableName();
        IntergrationQuery data = intergrationMapper.series(vo);
        DynamicDataSourceContextHolder.clearDataSourceType();
        return data;
    }

    @Override
    public List<MonitoredLatestQuery> scoreLast(List<String> monitoredCodeList) {
        List<MonitoredLatestQuery> list = monitoredMapper.itemsCode(monitoredCodeList);
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        for(MonitoredLatestQuery item: list){
            String tableName = ApiConfig.getTenant() + "_"+ item.getMonitoredCode() + "_tracing_intergration";
            List<Map> rl = intergrationMapper.intergrationLastData(tableName);
            item.setData(rl);
        }
        DynamicDataSourceContextHolder.clearDataSourceType();
        return list;
    }

    @Override
    public List<RobotLatestScoreQuery> scoreRobotList(List<String> robotCodes){
        List<RobotLatestScoreQuery> list = intergrationMapper.getRobotList(robotCodes);
        for(RobotLatestScoreQuery rScore: list){
            String robotCode = rScore.getRobotCode();
            List<ElementLatestScoreQuery> rChildren = rScore.getChildren();
            if(ObjectUtils.isNotEmpty(rChildren)){
                for(ElementLatestScoreQuery subChild: rChildren){
                    String tableName = subChild.getIntergrationName(
                            "002" ,"001"
                    );
                    try {
                        subChild.setData(intergrationLastData(tableName));
                    } catch (Exception e) {};
                }
            }
        }
        return list;
    }

    @Override
    public List<RobotLatestScoreQuery> scoreRobotIds(List<Long> robotIds){
        List<RobotLatestScoreQuery> list = intergrationMapper.scoreRobotIds(robotIds);
        for(RobotLatestScoreQuery rScore: list){
            String robotCode = rScore.getRobotCode();
            List<ElementLatestScoreQuery> rChildren = rScore.getChildren();
            if(ObjectUtils.isNotEmpty(rChildren)){
                for(ElementLatestScoreQuery subChild: rChildren){
                    String tableName = subChild.getIntergrationName(
                            "002" ,"001"
                    );
                    try {
                        subChild.setData(intergrationLastData(tableName));
                    } catch (Exception e) {};
                }
            }
        }
        return list;
    }

    private List<Map> intergrationLastData(String tableName) throws Exception{

        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<Map> list = intergrationMapper.intergrationLastData(tableName);
        DynamicDataSourceContextHolder.clearDataSourceType();
        return list;
    }
}
